package com.app.android.coliy.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;

import com.app.android.coliy.R;
import com.app.android.coliy.interface_.CommCallBack;
import com.app.android.coliy.tool.Log;
import com.app.android.coliy.util.AnimUtil;
import com.app.android.coliy.util.StatusBarUtil_Dialog;
import com.app.android.coliy.util.Util;

/**
 * Created by Administrator on 2018/8/29.
 */

public class PrintLoading_Dialog extends Dialog implements View.OnClickListener {
    public PrintLoading_Dialog(@NonNull Context context) {
        super(context, R.style.myDialog);
    }

    public PrintLoading_Dialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    protected PrintLoading_Dialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    LinearLayout ll_content;
    View view_bg;

    CommCallBack callBack;
    TextView tv_print;
    ImageView iv_img;
    ProgressBar progressbar, progressbar1;

    int count;   //文件总数
    int current;  //当前第几个

    public void setData(int count, int current) {
        this.count = count;
        this.current = current;
        Log.i("count:" + count + "  current:" + current);
    }

    //设置输入回调
    public void setIntputCallBack(CommCallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_printloading);
        initView();
        StatusBarUtil_Dialog.setImmersiveStatusBar(this, false);
    }

    private void initView() {
        ll_content = findViewById(R.id.ll_content);
        view_bg = findViewById(R.id.view_bg);
        view_bg.setOnClickListener(this);
        ll_content.setOnClickListener(this);
        AnimUtil.fadeIn(view_bg);
        AnimUtil.fadeIn(ll_content);

        iv_img = findViewById(R.id.iv_img);
        tv_print = findViewById(R.id.tv_print);
        progressbar = findViewById(R.id.progressbar);
        progressbar1 = findViewById(R.id.progressbar1);

    }

    public void setProgress(int progress) {
        Log.i("progress:" + progress);
        progressbar.setProgress(progress);
        if (count > 1) {
            tv_print.setText(Util.getString(R.string.printdialog_sending) + " (" + current + "/" + count + ")  " + progress + "%");
        } else {
            tv_print.setText(Util.getString(R.string.printdialog_sending) + "  " + progress + "%");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.view_bg:
//                dismissWithAnim();
                break;
            case R.id.ll_content:
                //不处理，只消耗点击事件
                break;
            default:
                break;
        }
    }

    public void dismissWithAnim() {
        if (view_bg.getAnimation() != null) return;
        AnimUtil.fadeOut(ll_content, null);
        AnimUtil.fadeOut(view_bg, new CommCallBack() {
            @Override
            public void onResult(Object obj) {
                dismiss();
            }
        });
    }

    @Override
    public void onBackPressed() {
        dismissWithAnim();
    }
}
